<?php
 global $objTemplate, $arrConfig,$sCanEdit,$lang;
  $sCanEdit = 1;  
    $objXajax->registerFunction("frmcreatetable_loadGrid");
    $objXajax->registerFunction("frmcreatetable_initPage");
    $objXajax->registerFunction("frmcreatetable_loadGrid");
    $objXajax->registerFunction("frmcreatetable_save");
    $objXajax->registerFunction("frmcreatetable_edit");
    $objXajax->registerFunction("frmcreatetable_initPage");
    $objXajax->registerFunction("frmcreatetalbe_create");
    $objXajax->registerFunction("frmcreatetalbe_drop");
  # start region process data     
  
  function frmcreatetable_loadGrid(){      

    global $lang,$objDbSelect,$path;
    $lang ='vi';
    $arrGrid = frmcreatetalbe_GetGrid();
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/hotel_createtable_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmcreatetable', 'innerHTML', $oTbs->Source); 
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmcreatetable_tbl');");
    return $objResponse->getXML();    
  }
  
  function  frmcreatetalbe_GetGrid(){
    global $objDbSelect;
    $sqlSelect = 'select * from tbl_tablescript';
    return $objDbSelect->GetArray($sqlSelect);
}

function frmcreatetalbe_create($from,$to){
     global $objDbSelect;
     $objResponse = new xajaxResponse();
     $sql = "SELECT * FROM tbl_tablescript WHERE  active = 1";
     $arr = $objDbSelect->GetArray($sql);
     $output ='';
     for($j>$from;$j<=$to;$j++){
        for($i=0;$i<count($arr);$i++){
          $table_name = $arr[$i]['table_name'];  
          $table_script = $arr[$i]['table_script']; 
          $sqlcreate =  str_replace($table_name,$table_name.'_'.$j,$table_script) ;
          // echo  $sqlcreate; die();
          $result = $objDbSelect->Execute($sqlcreate);
          if(!$result){
              $output .=  ",$table_name".'_'.$j;
          }          
        }    
     }
     $objResponse->addScript('changeWaiting(1);');
     return $objResponse->getXML();    
    // return $output;
}

function frmcreatetalbe_drop($from,$to){
     global $objDbSelect;
     $sql = "SELECT * FROM tbl_tablescript WHERE active = 1";
     $arr = $objDbSelect->GetArray($sql);
     $output ='';
     for($j>$from;$j<=$to;$j++){
        for($i=0;$i<count($arr);$i++){
          $table_name = $arr[$i]['table_name'];  
          $sqldrop = "DROP TABLE IF EXISTS $table_name"."_$j";
          $result = $objDbSelect->Execute($sqldrop);
          if(!$result){
              $output .=  ",$table_name".$j;
          }          
        }    
     }
     $objResponse->addScript('changeWaiting(1);');
     return $objResponse->getXML();    
     //return $output;
}

function frmcreatetable_save($param){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $tablename = $param['txtTableName'];
    $tablescript =addslashes($param['txtTableScript']) ;
    $edit = $param['hdEdit'];
    $id = $param['hdId'];
    if($edit!=1){
       $sql = "insert into tbl_tablescript(table_name,table_script) value('$tablename','$tablescript')" ; 
       //echo $sql; die();
       $objDbSelect->Execute($sql);
       $id_insert = mysql_insert_id();
        $objResponse->addScript("changeUpdate('frmcreatetable',$id_insert);");     
    }else{
       $sql = "UPDATE tbl_tablescript SET table_name='$tablename',table_script='$tablescript' WHERE id = $id"; 
       $objDbSelect->Execute($sql);
    }
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();      
}

function  frmcreatetable_edit($id){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $sql         = "select * from tbl_tablescript where id = '$id'";
    $arr         = $objDbSelect->GetArray($sql);
    
    $name_vi = $arr[0]['table_name'];
    $script = addslashes($arr[0]['table_script']);
       
    $objResponse->addScript("$('#frmcreatetable_txtTableName').val('$name_vi');");
    $objResponse->addScript("$('#frmcreatetable_txtTableScript').val('$script');"); 
    $objResponse->addScript('changeWaiting(1);');
    
    return $objResponse->getXML();
} 
  
  # end region process data
?>
